/**
 * @param {character[][]} board
 * @return {void} Do not return anything, modify board in-place instead.
 */
var solve = function (board) {
  const m = board.length;
  if (m === 0) return;
  const n = board[0].length;

  const directions = [[-1, 0], [1, 0], [0, -1], [0, 1]];

  const markBorder = (i, j) => {
    if (i < 0 || i >= m || j < 0 || j >= n || board[i][j] !== 'O') {
      return;
    }
    board[i][j] = '#';
    for (const [dx, dy] of directions) {
      markBorder(i + dx, j + dy);
    }
  }

  for (let j = 0; j < n; j++) {
    if (board[0][j] === 'O') markBorder(0, j);
    if (board[m - 1][j] === 'O') markBorder(m - 1, j);
  }
  for (let i = 0; i < m; i++) {
    if (board[i][0] === 'O') markBorder(i, 0);
    if (board[i][n - 1] === 'O') markBorder(i, n - 1);
  }

  for (let i = 0; i < m; i++) {
    for (let j = 0; j < n; j++) {
      if (board[i][j] === 'O') {
        board[i][j] = 'X';
      } else if (board[i][j] === '#') {
        board[i][j] = 'O';
      }
    }
  }
};